﻿Public Class clsPosition

    Private _Speed As Double
    Public Property Speed() As Double
        Get
            Return _Speed
        End Get
        Private Set(ByVal value As Double)
            _Speed = value
        End Set
    End Property

    Private _SatCount As Integer
    Public Property SatCount() As Integer
        Get
            Return _SatCount
        End Get
        Private Set(ByVal value As Integer)
            _SatCount = value
        End Set
    End Property

    Private _PositionAccuracy As Double
    Public Property PositionAccuracy() As Double
        Get
            Return _PositionAccuracy
        End Get
        Private Set(ByVal value As Double)
            _PositionAccuracy = value
        End Set
    End Property

    Private _SignalStrength As Double
    Public Property SignalStrength() As Double
        Get
            Return _SignalStrength
        End Get
        Private Set(ByVal value As Double)
            _SignalStrength = value
        End Set
    End Property

    <CLSCompliant(False)> _
    Public Sub New(ByVal GPSPosition As Microsoft.WindowsMobile.Samples.Location.GpsPosition)
        Try
            If GPSPosition.SpeedValid Then
                If _MobileSettings.Unit = enUnit.Metric Then
                    Me.Speed = GPSPosition.Speed * _MobileSettings.KNOCK_TO_METRIC
                Else
                    Me.Speed = GPSPosition.Speed * _MobileSettings.KNOCK_TO_MILE
                End If
            End If

            If GPSPosition.SatelliteCountValid Then
                Me.SatCount = GPSPosition.SatelliteCount
                Me.SignalStrength = Me.PopulateSignalStrength(GPSPosition.GetSatellitesInView)
            End If

            If GPSPosition.PositionDilutionOfPrecisionValid Then
                Me.PositionAccuracy = Me.PopulateAccuracy(GPSPosition.PositionDilutionOfPrecision)
            End If
        Catch ex As Exception
            PushLog("Error in clsPosition.New: " & ex.Message)
        End Try
    End Sub

    Private Function PopulateSignalStrength(ByVal SatInView() As Microsoft.WindowsMobile.Samples.Location.Satellite) As Double
        Try
            If SatInView Is Nothing OrElse SatInView.Count = 0 Then
                Return 0
            End If
            Dim rtnVal As Double = 0
            For Each Sat In SatInView
                rtnVal += Sat.SignalStrength
            Next
            Return rtnVal / SatInView.Count
        Catch ex As Exception
            PushLog("Error in clsPosition.PopulateSignalStrength: " & ex.Message)
        End Try
    End Function

    Private Function PopulateAccuracy(ByVal PositionDilutionOfPrecision As Double) As Double
        Try
            Dim a As Double
            Dim b As Double
            a = -99 / 49
            b = 100 + a
            Return a * PositionDilutionOfPrecision + b
        Catch ex As Exception
            PushLog("Error in clsPosition.PopulateAccuracy: " & ex.Message)
        End Try
    End Function

End Class
